package io.github.mymonstercat.utils;

import com.benjaminwan.ocrlibrary.Point;
import com.benjaminwan.ocrlibrary.TextBlock;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.mymonstercat.entity.HwResult;
import io.github.mymonstercat.entity.HwWordsBlock;
import io.github.mymonstercat.entity.OCRStandardRes;
import com.fasterxml.jackson.core.type.TypeReference;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TextInfoTransformer {

    // 百度OCR识别结果转换到标准格式
    public OCRStandardRes baiduOCRTransform(ArrayList<TextBlock> baiduResult){
        OCRStandardRes ocrStandardRes = new OCRStandardRes();
        List<OCRStandardRes.wordsBlock> wordsBlockList = new ArrayList<>();

        for (TextBlock textBlock : baiduResult) {
            OCRStandardRes.wordsBlock wordsBlock = new OCRStandardRes.wordsBlock();
            wordsBlock.setWords(textBlock.getText());
            wordsBlock.setConfidence(textBlock.getBoxScore());

            // 转换 Point 到 List<List<Double>>
            List<List<Double>> location = new ArrayList<>();
            List<Double> pointList = new ArrayList<>();
            for (Point point : textBlock.getBoxPoint()) {
                pointList.add((double) point.getX());
                pointList.add((double) point.getY());
            }
            location.add(pointList); // 假设只需要一个框的坐标

            wordsBlock.setLocation(location);
            wordsBlockList.add(wordsBlock);
        }

        ocrStandardRes.setWordsBlockList(wordsBlockList);
        return ocrStandardRes;
    }

    // 华为OCR识别结果转换到标准格式
    public OCRStandardRes hwOCRTransform(HwResult hwResult){
        OCRStandardRes ocrStandardRes = new OCRStandardRes();
        ocrStandardRes.setWordsBlockList(new ArrayList<>());
        // 将 wordsBlockList 中的值复制到 wordsBlockList2 中
        for (HwWordsBlock block : hwResult.getResult().getWordsBlockList()) {
            OCRStandardRes.wordsBlock newBlock = new OCRStandardRes.wordsBlock();
            newBlock.setWords(block.getWords());
            newBlock.setConfidence(block.getConfidence());
            newBlock.setLocation(block.getLocation());
            ocrStandardRes.getWordsBlockList().add(newBlock);
        }
        return ocrStandardRes;
    }

    // 从数据库中获取的模板文本块的内容和坐标转换到标准格式
    public OCRStandardRes sqlResultTransform(List<String> ocrtexts, List<String> ocrlocations) throws JsonProcessingException {
        // 初始化 OCRStandardRes 对象
        OCRStandardRes ocrStandardRes = new OCRStandardRes();
        ocrStandardRes.setWordsBlockList(new ArrayList<>());

        // 创建 ObjectMapper 实例用于解析 JSON 字符串
        ObjectMapper objectMapper = new ObjectMapper();

        // 遍历 ocrlocations 和 ocrtexts 并赋值
        for (int i = 0; i < ocrlocations.size(); i++) {
            // 解析字符串为 List<List<Double>>
            String locationStr = ocrlocations.get(i);
            List<List<Double>> location = objectMapper.readValue(locationStr, new TypeReference<List<List<Double>>>() {});

            // 获取对应的文字
            String words = ocrtexts.get(i);

            // 创建新的 wordsBlock 对象并赋值
            OCRStandardRes.wordsBlock block = new OCRStandardRes.wordsBlock();
            block.setLocation(location);
            block.setWords(words);

            // 将 wordsBlock 添加到 OCRStandardRes 对象中
            ocrStandardRes.getWordsBlockList().add(block);
        }
        return ocrStandardRes;
    }

    public static List<List<Double>> mapStringToLocation(String str) {
        List<List<Double>> location = new ArrayList<>();
        Pattern pattern = Pattern.compile("\\[(\\d+\\.?\\d*),\\s*(\\d+\\.?\\d*)\\]");
        Matcher matcher = pattern.matcher(str);

        while (matcher.find()) {
            Double x = Double.parseDouble(matcher.group(1));
            Double y = Double.parseDouble(matcher.group(2));
            List<Double> point = new ArrayList<>();
            point.add(x);
            point.add(y);
            location.add(point);
        }

        return location;
    }

}
